<?php
/**
 * User: hjd-yao
 * Date: 2019/7/10
 * Time: 10:04
 */

namespace Shop\Http\Controllers\Pc;


use App\Models\Shop\ShopCategoryModel;
use App\Models\Shop\ShopGoodsModel;
use App\Models\Shop\UserAddressModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Shop\Http\Controllers\BaseController;
use Illuminate\Support\Facades\DB;

class GoodsController extends BaseController
{
    public function goodsList(Request $request,$zone = 1)
    {
        $goodsModel = ShopGoodsModel::where('status',1);

        $goodsModel = $this->handleGoodsSearch($goodsModel,$request)->where('goods_zone',$zone);
        $sort = $request->input('psort',0);
        if($sort == 1){//价格升序
            $goodsModel = $goodsModel->orderBy('price');
        }elseif ($sort==2){ // 价格降序
            $goodsModel = $goodsModel->orderBy('price','desc');
        }elseif ($sort == 3){ //销量降序
            $goodsModel = $goodsModel->orderBy('show_sales_volume','desc');
        }elseif ($sort == 4){//时间降序
            $goodsModel = $goodsModel->orderBy('created_at','desc');
        }
        //dd($request->input());

        $list = $goodsModel->paginate($this->perNum(15));

        return $this->pcRender('goods.list',[
            'list'=>$list,
            'psort'=>$sort,
            'zone'=>$zone,
            'request'=>$request->input()
        ]);
    }


    public function search(Request $request)
    {
        $keyword = $request->input('keyword');

        $goodsModel = ShopGoodsModel::where('status',1);

        $goodsModel = $this->handleGoodsSearch($goodsModel,$request);

        if(!empty($keyword)){
            $goodsModel = $goodsModel->where(DB::raw('concat(title,goods_name,description,keywords)'),'like','%'.$keyword.'%');
        }

        $sort = $request->input('psort',0);
        if($sort == 1){//价格升序
            $goodsModel = $goodsModel->orderBy('price');
        }elseif ($sort==2){ // 价格降序
            $goodsModel = $goodsModel->orderBy('price','desc');
        }elseif ($sort == 3){ //销量降序
            $goodsModel = $goodsModel->orderBy('show_sales_volume','desc');
        }elseif ($sort == 4){//时间降序
            $goodsModel = $goodsModel->orderBy('created_at','desc');
        }
        //dd($request->input());

        $list = $goodsModel->paginate($this->perNum(15));

        return $this->pcRender('goods.list',[
            'list'=>$list,
            'psort'=>$sort,
            'keyword'=>$keyword,
            'request'=>$request->input()
        ]);

    }

    private function handleGoodsSearch(Builder $goods, Request $request)
    {
        $price = $request->input('price',0);
        if($price){
            $prices = str2arr($price,'-',false);
            if(is_array($prices)){
                $goods = $goods->whereBetween('price',$prices);
            }else{
                $goods = $goods->where('price','>=',$prices);
            }
        }

        $cate = $request->input('cate',0);
        if($cate>0){
            $goods = $goods->whereRaw("FIND_IN_SET(?,`category_ids`)",[$cate]);
        }

        return $goods;
    }

    public function goodsDetails(Request $request,$goods_id)
    {
        $goods = ShopGoodsModel::find($goods_id);
        $cate_titles = ShopCategoryModel::whereIn('id',str2arr($goods->category_ids))->pluck('name_mobile','id');
        return $this->pcRender('goods.detail',[
            'goods'=>$goods,
            'cate_titles'=>$cate_titles
        ]);
    }

    public function detailAdGoods()
    {
        $goods = ShopGoodsModel::where('status',1)->inRandomOrder()->limit(8)->get();
        return response()->json(['status'=>1,'data'=>$goods]);
    }

}